package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.up.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.up.HomePageUserTempMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.up.HomePageUserTemp;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.up.HomePageUserTempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by ${yangsy} on 2017/3/23.
 */
@Service
public class HomePageUserTempServiceImpl implements HomePageUserTempService {
    @Autowired
    private HomePageUserTempMapper homePageUserTempMapper;

    @Override
    public List<HomePageUserTemp> queryList(HomePageUserTemp homePageUserTemp) {
        return homePageUserTempMapper.queryListNotpaging(homePageUserTemp);
    }

    @Override
    public int insert(HomePageUserTemp homePageUserTemp) {
        return homePageUserTempMapper.insertSelective(homePageUserTemp);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insert(String saveItems, String userId) {
        int resultCount = 0;
        JSONArray aprvArr = JSONArray.parseArray(saveItems);
        if (aprvArr == null || aprvArr.size() <= 0) {
            return resultCount;
        }
        homePageUserTempMapper.delete(userId);
        int len = aprvArr.size();
        //  循环遍历插入
        for (int i = 0; i < len; i++) {
            JSONObject aprv = aprvArr.getJSONObject(i);
            boolean falg = aprv.getBoolean("isDisplay");
            if (!falg) {
                continue;
            }
            HomePageUserTemp userTemp = new HomePageUserTemp();
            //  主键
            userTemp.setId(UniqueUtil.uuid());
            // 模板Id
            userTemp.setBasictempId(aprv.getString("id"));
            //  x
            userTemp.setX(aprv.getBigDecimal("x"));
            // y
            userTemp.setY(aprv.getBigDecimal("y"));
            // height
            userTemp.setHeight(aprv.getBigDecimal("height"));
            //  width
            userTemp.setWidth(aprv.getBigDecimal("width"));
            //  对应用户
            userTemp.setUserid(userId);
            //  do  insert
            homePageUserTempMapper.insertSelective(userTemp);
        }
        return resultCount;
    }

    @Override
    public int delete(String userId) {
        return homePageUserTempMapper.delete(userId);
    }
}
